<?php

// 无同步、同步失败、多项同步有失败项、成功同步
$id = abs(intval($_GET['id']));

$order = Table::Fetch('order', $id);
if (!$order) { 
	Session::Set('error', '订单不存在');
	redirect( WEB_ROOT . '/index.php' );
}

/* publish message */
if($order['remark'] && $order['remark'] != ''){
	$remarks = explode("|||", $order['remark']);
	$publishedArr = LSync::Publish($login_user_id, $user_network_weights, $remarks[0], $remarks[1], $remarks[2]);
	$table = new Table('synced', array(
		'order_id' => $id,
		'result' => json_encode($publishedArr),
		'create_time' => time(),
	));
	$table->insert(array('order_id', 'result', 'create_time'));
	/* End */
	
	
	if($order['state'] == 'unpay'){
		Table::UpdateCache('order', $id, array('state' => 'sync'));
		$order = Table::FetchForce('order', $id);
		ZTeam::BuyOne($order);
	}
	
	$team = Table::Fetch('team', $order['team_id']);
}
/*********************************************************************************************************/
$order = Table::Fetch('order', $id);
if (!$order) { 
	Session::Set('error', '订单不存在');
	redirect( WEB_ROOT . '/index.php' );
}

// 成功获取到优惠券
//if($order['state'] == 'unpay'){
	Table::UpdateCache('order', $id, array('state' => 'sync'));
	$order = Table::FetchForce('order', $id);
	ZTeam::BuyOne($order);
//}

$team = Table::Fetch('team', $order['team_id']);

Session::Set('notice', '分享成功，稍后您的手机将接收到代金券。');
$html = render('ajax_dialog_notice');
json($html, 'dialog');
/*********************************************************************************************************/
//include template('order_synced');
/*********************************************************************************************************/
//redirect(WEB_ROOT  . "/order/synced.php?id={$id}");

